﻿using Ledict.Core.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ledict.ConditionExtract.Common
{
  public static  class PublicMethod
    {
        public static string UserName = string.Empty;

        private static string DbName = "JudgeDoc";

        /// <summary>
        /// 获取表下面的所有列
        /// </summary>
        /// <param name="dataBaseName"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static string GetColumnNameByTable(string dataBaseName, string tableName)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                Database db = DatabaseFactory.CreateDatabase(DbName);
                string strSql = string.Format(@" USE {0} ;SELECT COLUMN_NAME as name FROM INFORMATION_SCHEMA.columns WHERE TABLE_NAME='{1}' ; ", dataBaseName, tableName);
                DbCommand oCmd = db.GetSqlStringCommand(strSql.ToString());
                DataSet table = db.ExecuteDataSet(oCmd);
                if (table != null && table.Tables[0] != null && table.Tables[0].Rows.Count > 0)
                {
                    foreach (DataRow dr in table.Tables[0].Rows)
                    {
                        sb.Append("[" + dr["name"] + "],");
                    }
                    return sb.ToString();
                }
                else
                {
                    return "";
                }              
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// datatable去重
        /// </summary>
        /// <param name="dtSource">筛选去重的datatable</param>
        /// <param name="columnNames">依据哪些列去重</param>
        /// <returns></returns>
        public static DataTable GetDistinctTable(DataTable dtSource, params string[] columnNames)
        {
            DataTable distinctTable = dtSource.Clone();
            try
            {
                if (dtSource != null && dtSource.Rows.Count > 0)
                {
                    DataView dv = new DataView(dtSource);
                    distinctTable = dv.ToTable(true, columnNames);
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("去重DataTable出错："+ex.Message);
            }
            return distinctTable;
        }

        // Base64 解码  
        /// <summary>  
        /// 对Base64编码的字符串进行解码  
        /// </summary>  
        /// <param name="codepage">代码页,utf-8:65001，gb2132:936</param>  
        /// <param name="encode">需要解码的字符串</param>  
        /// <returns></returns>  
        public static string DeCodeBase64new(int codepage, string encode)
        {
            string decode = "";
            try
            {
                byte[] bytes = Convert.FromBase64String(encode);
                decode = Encoding.GetEncoding(codepage).GetString(bytes);
            }
            catch(Exception ex)
            {
                decode = encode;
            }

            return decode;
        }

        /// <summary>
        /// 去除
        /// </summary>
        /// <param name="dv"></param>
        public static void ClearDataGridViewData(DataGridView dv)
        {
            DataTable dt = (DataTable)dv.DataSource;
            if (dt != null)
            {
                dt.Rows.Clear();
                dv.DataSource = dt;
            }
        }

        /// <summary>
        /// 给DataTable添加序号列，名称为Num
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DataTable AddDataTableNum(DataTable dt)
        {
            if (dt != null && dt.Rows.Count > 0)
            {
                dt.Columns.Add("Num");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["Num"] = i + 1;
                }
                return dt;
            }
            else
            {
                return dt;
            }
        }

        /// <summary>
        /// 判断文本框是否为空，如果为空返回FALSE及自动定位到文本框否则返回true
        /// </summary>
        /// <param name="tb">文本框控件的名称</param>
        /// <returns>为空返回FALSE 有值返回true</returns>
        public static bool IsTextBoxRight(TextBox tb)
        {
            if (string.IsNullOrEmpty(tb.Text.Trim()))
            {
                MessageBox.Show("请先补充完整文本框后再试！");
                tb.Focus();
                return false;
            }
            return true;
        }
    }
}
